package com.zhangmengyuan.medical.controller;

import com.zhangmengyuan.medical.entity.User;
import com.zhangmengyuan.medical.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller/*这个类是处理请求的类。*/
@RequestMapping("user")/*处理请求路径*/
public class DemoController {
  /**
   * redirect:
   */
  @Autowired/*自动实例化*/
  private UserService userService;


  //添加

  public String save(User user, Model model) {
    return "redirect:/user/all";//跳转到
  }


  //更新
  public String update(User user, @PathVariable("userId") Integer userId, Model model)/*路径传参*/
  {/*Intger代表类型，代表这个id必须是整数*/
    user.setUserId(userId);
    this.userService.save(user);
    return "redirect:/user/all";
  }

  public String getUser(Model model) {
    List<User> userList = this.userService.getAll();/*页面传参*/
   /* 在 JSP 视图页面中通过 request.getAttribute(“customerList”)
   或者通过 ${customerList} EL 表达式访问模型对象中的 customerList对象*/
    model.addAttribute("userList", userList);
    return "defaut_user";/*在方法上直接return一个字符串，就会对应webapp/WEB-INF.jsp/下的文件，不需要index.jsp*/
  }

  public String getUserById(@PathVariable("userId") Integer userId, Model model) {
    User user = this.userService.getById(userId);
    model.addAttribute("user", user);
    return "redirect:/user/all";
  }

  /*@GetMapping("/delete/{userId}")
  public String deleteUserById(@PathVariable("userId") Integer userId,Model model){
    User user = this.userService.getById(userId);
    this.userService.deleteUserById(user);
    return "detil";

  }*/


  @GetMapping("/")
  public String toDetil(Model model) {
    return "defaut_user";
  }

}


